home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / cljune86.zip / BUMP.C next >
Text File  |  1986-03-31  |  7KB  |  194 lines

  1. #include "stdio.h"
  2. #include "dos.h"
  3.  
  4. #define BUF_SIZE 512
  5. char    buf[2 * BUF_SIZE],    /*------- Input text file buffer -------*/
  6.             s_buf[BUF_SIZE];
  7.  
  8. /******************************************************************************/
  9. /*----------------------------- The BUMP utility -----------------------------*/
  10. /******************************************************************************/
  11. /*                                                                            */
  12. /*  Name: bump                           Version: 1.3                         */
  13. /*                                                                            */
  14. /*  Last Edit: 03/21/86                  Compiler: Lattice C, v2.15d          */
  15. /*                                                                            */
  16. /*  Purpose:  Take a passed name with embedded number and add one to the      */
  17. /*            number.  Search specified files for references to original      */
  18. /*            name and change to references to new name, (with bumped number).*/
  19. /*                                                                            */
  20. /******************************************************************************/
  21. /*------------------ Copyright 1985 & 1986, by Gary Elfring ------------------*/
  22. main(argc,argv)
  23.     int        argc;
  24.     char    *argv[];
  25.     {
  26.     extern    FILE    *fopen();
  27.     extern    char    *stpchr();
  28.     extern    long    ftell();
  29.     extern    char    *calloc();
  30.  
  31.     FILE    *in_file;
  32.  
  33.     char *t,*s,*data,*old;
  34.     char    file_old[80],
  35.                 file_new[80];
  36.     char    *p,
  37.                 ext[4],
  38.                 b_name[9],        /*------ Part of file name before embedded number ------*/
  39.                 e_name[9],        /*------- Part of file name after embeded number -------*/
  40.                 num[9],                /*--------- ASCII number embeded in file name ----------*/
  41.                 control[10];
  42.  
  43.     int        i, j,                    /*------------------- Work counters --------------------*/
  44.                 length,
  45.                 cnt,                    /*------------ Count of number char in name ------------*/
  46.                 position,            /*--------- Flag to tell where in name we are ----------*/
  47.                 bytes_read,
  48.                 found;
  49.  
  50.     unsigned    file_point;
  51.  
  52.     long    pos;
  53.  
  54. /*----- Give them some help if they don't know how to invoke the program -----*/
  55.     if (argc < 3)
  56.         {
  57.         printf("BUMP searches a file or series of files for a specific file name.\n");
  58.         printf("This name must end in a number.  Anytime the file name is found the\n");
  59.         printf("number portion will be increased by one.\n\n");
  60.         printf("USE --> bump file1 file2 file3 ... fileN nameXX.ext{cr}\n");
  61.         printf("        where file1-N are files to search\n");
  62.         printf("        and nameXX.ext is any file name with the name part ending\n");
  63.         printf("        in a 1 to 4 digit number.\n");
  64.         exit(1);
  65.         }
  66.  
  67. /*-------------------------- Get file name to bump ---------------------------*/
  68.     strcpy(file_old,argv[argc - 1]);
  69.  
  70. /*--------------------------- Rip name into parts ----------------------------*/
  71. /*----------------------- Fool with extension if found -----------------------*/
  72.     if ((p = stpchr(file_old,'.')) != NULL)
  73.         {
  74.         *p++ = 0;
  75.         strcpy(ext,p);
  76.         }
  77.     else
  78.         ext[0] = 0;
  79.  
  80. /*------------------------ Need length of whats left -------------------------*/
  81.     length = strlen(file_old);
  82.  
  83. /******************************************************************************/
  84. /*-------------- Now rip whats left into numer and other parts ---------------*/
  85. /******************************************************************************/
  86.     b_name[0] = e_name[0] = num[0] = cnt = 0;
  87.     position = 0;
  88.     for (i = 0, t = &file_old[0], p = &b_name[0]; i < length; ++i)
  89.         {
  90.         /*-------------- If we havent found any numbers in name yet --------------*/
  91.         if (position == 0)
  92.             {
  93.             /*------------------- Is this the first digit found --------------------*/
  94.             if (isdigit(*t))
  95.                 {
  96.                 *p = 0;                /*-------------- End the beginning string --------------*/
  97.                 ++cnt;
  98.                 ++position;
  99.                 p = &num[0];    /*------ Shift the pointer to the number storage -------*/
  100.                 }
  101.             }
  102.         /*------------- Else if we already found a digit in the past -------------*/
  103.         else if (position == 1)
  104.             {
  105.             /*--------------------- See if still doing digits ----------------------*/
  106.             if (isdigit(*t))
  107.                 ++cnt;
  108.             else        /*--------- If we just stopped finding digits, switch ----------*/
  109.                 {
  110.                 *p = 0;
  111.                 ++position;
  112.                 p = &e_name[0];  /*----------- Move poiter to end storage ------------*/
  113.                 }
  114.             }
  115.         *p++ = *t++;  /*------------ ALWAYS save character somewhere -------------*/
  116.         }
  117.     *p = 0;
  118.  
  119.     strcpy(file_old,argv[argc - 1]);
  120.     /*---- We need to build a format control string with # of digits in it -----*/
  121.     sprintf(control,"%%s%%0%dd%%s",cnt);
  122.     /*-------------------- Now build the "bumped" file name --------------------*/
  123.     sprintf(file_new,control,b_name,atoi(num) + 1,e_name);
  124.     if (ext[0] != 0)
  125.         {
  126.         strcat(file_new,".");
  127.         strcat(file_new,ext);
  128.         }
  129.     printf("Changing all reference to %s to %s in files:\n",file_old,file_new);
  130.     for (i = 1; i < (argc - 1); ++i)
  131.         printf("%s ",argv[i]);
  132.     printf("\n");
  133.  
  134. /*---------------------- While there are files to bump -----------------------*/
  135.     for (i = 1; i < (argc - 1); ++i)
  136.         {
  137.         found = 0;
  138.         /*----------------- Make sure the file to search exists ------------------*/
  139.         if ((in_file = fopen(argv[i],"rb+")) == NULL)
  140.             {
  141.             printf("Error: can't open file %s\n",argv[i]);
  142.             exit(1);
  143.             }
  144.  
  145.         /*------------------ File size must be less than 64000 -------------------*/
  146.         fseek(in_file,0L,2);
  147.         if ((pos = ftell(in_file)) > 64000L)
  148.             {
  149.             fclose(in_file);
  150.             printf("Sorry, can not work with files larger than 64000 bytes\n");
  151.             exit(1);
  152.             }
  153.         fseek(in_file,0L,0);
  154.  
  155.         /*----------------- Allocate the storage for search area -----------------*/
  156.         if ((data = calloc((unsigned)pos,1)) == NULL)
  157.             {
  158.             fclose(in_file);
  159.             printf("Sorry, can not allocate enough memory\n");
  160.             exit(1);
  161.             }
  162.  
  163.         /*------------------------- Read in entire file --------------------------*/
  164.         bytes_read = fread(data,1,(unsigned)pos,in_file);
  165.  
  166.         /*--------------- Look through entire file for all matches ---------------*/
  167.         for (file_point = 0; file_point < bytes_read - length; ++file_point)
  168.             {
  169.             t = old = &data[file_point];
  170.             s = &file_old[0];
  171.           for ( ; *t == *s; ++t)
  172.                 {
  173.                 if(!*++s)
  174.                     {
  175.                     /*------------------------- Found a match! -------------------------*/
  176.                     found = !0;
  177.                     for (j = 0; j < length; ++j)
  178.                         *old++ = file_new[j];      /*------- Replace with new name -------*/
  179.                     }
  180.                 }  /*---------------------- End for char = char ----------------------*/
  181.             }  /*------------------------- End the search --------------------------*/
  182.  
  183.         if (!found)
  184.             printf("Could not find %s in file %s\n",file_old,argv[i]);
  185.         else
  186.             {
  187.             fseek(in_file,0L,0);
  188.             fwrite(data,bytes_read,1,in_file);
  189.             }
  190.         fclose(in_file);
  191.         free(data);
  192.         }  /*---------------------- End while text to read -----------------------*/
  193.     }
  194.